package com.nzy.tree;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ViewBuilder {



    private static List<View> list = new ArrayList<>();

    private static long incr = 1;

    public static void main1(String[] args) {
        String json = read("C:\\Users\\Administrator\\Desktop\\view.json");
        parseView(json);
        for(View vo:list){
            System.out.println(JSON.toJSONString(vo));
        }
    }

    public static void main(String[] args) {
        String json = read("C:\\Users\\Administrator\\Desktop\\new.json");
        parseView(json);
        for(View vo:list){
            if(vo.getAppId() == null) {
                System.out.println(JSON.toJSONString(vo));
            }
        }
    }


    public static String read(String path) {
        String json = "";
        File file = new File(path);
        try {
            FileReader fileReader = new FileReader(file);
            Reader reader = new InputStreamReader(new FileInputStream(file), "Utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            json= sb.toString();
        }catch (Exception e){

        }
        return json;

    }

    public static void parseView(String json){
        List<JSONObject> jsonArray = JSON.parseArray(json, JSONObject.class);
        parseView(jsonArray, -1L);
    }

    public static void parseView(List<JSONObject> jsonArray, Long parentId){
       for(int i = 0; i< jsonArray.size(); i++){
           JSONObject jsonObject = jsonArray.get(i);
           Long id = incr;
           View view = parseVo(jsonObject, id, parentId);
           list.add(view);
           incr++;
           if(jsonObject.containsKey("children")){
               List<JSONObject> json = JSON.parseArray(jsonObject.getString("children"), JSONObject.class);
               parseView(json, id);
           }
           if(jsonObject.containsKey("buttons")){
               List<JSONObject> json = JSON.parseArray(jsonObject.getString("buttons"), JSONObject.class);
               parseView(json, id);
           }
       }
    }

    public static View parseVo(JSONObject jsonObject, Long id, Long parentId){
        View vo = new View();
        vo.setId(id);
        vo.setAppId(jsonObject.getString("appId"));
        if(parentId == null){
            vo.setParentId(-1L);
        }else {
            vo.setParentId(parentId);
        }
        vo.setName(jsonObject.getString("name"));
        vo.setHidden(0);
        vo.setKeepAlive(0);
        vo.setViewLevel(0);
        vo.setStatus(0);
        vo.setNum(id.intValue());
        vo.setUrl("");
        Date date = new Date();
        vo.setCreateTime(date);
        vo.setUpdateTime(date);
        if(jsonObject.containsKey("key")){//按钮
            vo.setTitle(jsonObject.getString("name"));
            vo.setName(jsonObject.getString("name"));
            vo.setType(-2);
        }else {
            vo.setPath(jsonObject.getString("path"));
            vo.setComponent(jsonObject.getString("component"));
            vo.setIcon("");
            vo.setTitle(jsonObject.getString("title"));
            vo.setType(jsonObject.getInteger("type"));
        }
        return vo;
    }


}
